<?php

/**
 * debug 测试模拟更新加油包曝光量
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\bizAd\AdPlanDataFlow as mAdPlanDataFlow;
use Biz\service\bizAd\AdPlan;
use Doraemon\model\Flow\FlowPackage as mFlowPackage;
use Doraemon\tools\HhzEnv;

$id = $argv[1] ?? 0;//流量加油包id
$type = $argv[2] ?? 'one';
$num = $argv[3] ?? 1;
var_dump('加油包id:' . $id . '#加量:' . $num);
try {
    if (HhzEnv::isTest() || HhzEnv::isShopTest()) {
        if ($id) {
            if ($num) {
                if ($type == 'one') {
                    for ($i = 0; $i < $num; $i++) {
                        $info = getInfo($id);
                        var_dump('测试+' . ($i + 1));
                        $count = $info['real_exposure'] + 1;
                        var_dump('加量后：' . $count);
                        mFlowPackage::update(['real_exposure' => $count], ['id' => $id])->rowCount();
                        AdPlan::changeExposeFromFlow($info);
                        sleep(1);
                    }
                } else if ($type == 'more') {
                    var_dump('测试+' . $num);
                    $info = getInfo($id);
                    $count = $info['real_exposure'] + $num;
                    var_dump('加量后：' . $count);
                    mFlowPackage::update(['real_exposure' => $count], ['id' => $id])->rowCount();
                    AdPlan::changeExposeFromFlow($info, $num);
                }
                var_dump('测试完成');
            }
        }
    } else {
        var_dump('当前环境不支持');
    }
} catch (\Exception $e) {
    var_dump(['code' => $e->getCode(), 'message:' . $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
}

function getInfo($id)
{
    $info = mFlowPackage::get('*', ['id' => $id]);
    $adPlanDataFlow = mAdPlanDataFlow::getListByWhere(['flow_package_id' => $id]);
    if (empty($info) || empty($adPlanDataFlow)) {
        var_dump('不存在的流量加油包');
        throw new \Exception('不存在的流量加油包');
    }
    return $info;
}